import 'dart:io';

import 'package:bot_toast/bot_toast.dart';
import 'package:easy_read_comic/controller/me/me_controller.dart';
import 'package:get/get.dart';
import 'package:path_provider/path_provider.dart';

import '../app_controller.dart';

class SettingController extends GetxController {
  //缓存大小
  int cacheTotal = 0;

  @override
  void onInit() {
    super.onInit();
    this.getCacheTotal();
  }

  // cleanCache 清理缓存
  void cleanCache() async {
    try {
      Directory cacheDir = await getTemporaryDirectory();
      await cacheDir.delete(recursive: true);
    } finally {
      Future.delayed(Duration(seconds: 1), () {
        BotToast.showSimpleNotification(
          title: "清理完成",
        );
        this.cacheTotal = 0;
        this.update();
      });
    }
  }

  // getCacheTotal 获取缓存
  void getCacheTotal() async {
    Directory tempDir = await getTemporaryDirectory();
    await for (FileSystemEntity entity
        in tempDir.list(recursive: true, followLinks: false)) {
      if (entity is File) {
        cacheTotal += await entity.length();
      }
    }
    this.update();
  }

  // exitAccount 推出账号登录
  void exitAccount() {
    AppController().cleanAppDataAndSync().then((value) {
      if (value) {
        //重新渲染当前页面和我的页面
        var meController = Get.put(MeController());
        update();
        meController.update();
      }
    });
  }
}
